import {Component} from "react";
import React from "react";
import API from '../untils/api';
import {connect} from 'react-redux'
import store from "../store/store";
import {is, fromJS} from 'immutable'
import List from '../components/List'
import SwiperSlider from "../components/swiperSlider";
import Untils from '../untils/untils'
class Do extends Component {
    constructor(props) {
        super(props);
        this.state = {
            list: [],
            gold: 1,
            page: 1,
            firstComing: true
        }
    }

    render() {
        return (
            <div className='content' >
                <SwiperSlider/>
                <List list={this.state.list} path={this.props.location.pathname} getList={this.getList.bind(this)}/>
            </div>
        )
    }

    componentDidMount() {
        this.getList(1, 1)
    }

    shouldComponentUpdate(nextProps, nextState) {
        return !is(fromJS(this.props), fromJS(nextProps)) || !is(fromJS(this.state), fromJS(nextState));

    }

    getList(gold, page, flag) {

        if(flag){
            this.setState({
                list: [],
                page:1
            });
        }else {
            this.setState({
                page:page
            })
        }
        let params = {
            data: {
                gold: gold,
                currentPage: this.state.page
            },
            url: '/huoban-sales/http/sales/function/product/findGoldProductList.do',
            showLoading: this.state.firstComing,
        };
        API.request(params).then((res) => {
            let newArr = this.state.list;
            let arr = newArr.concat(res.data.list);
            this.setState({
                list: arr,
                firstComing: false
            });
            store.dispatch({
                type: 'GET_LIST',
                list: res.data.list
            })
        });
    }

    componentWillMount() {

        let firstComing=Untils.getLocalStorage('firstComing');

        if(!firstComing){
            Untils.setLocalStorage('firstComing',1);
            this.props.history.replace('/advertise');
        }

        this.props.sendStatus();
    }
}


const mapDispatchToProps = dispatch => ({
    sendStatus: message => {
        dispatch({type: 'SHOW_HEAD'});
        dispatch({type: 'SHOW_FOOT'});
    },
});

export default connect(
    null,
    mapDispatchToProps
)(Do);

